app.controller('LoginCtrl', ['$scope', '$rootScope', 'httpLoad', '$state', '$http',function($scope, $rootScope, httpLoad, $state,$http) {
  $scope.user = {};
  $scope.remember = '';
  $rootScope.link = '/statics/css/login.css';
  $scope.url = "/captcha";
  var userData = JSON.parse(localStorage.getItem('bycUserData'));
  if (userData != null) {
    $scope.user.username = userData.username;
    $scope.remember = userData.remember;
    if($scope.remember) $scope.user.password = userData.password;
  }
  //登陆接口
  $scope.login = function() {
    var user = {
      username: $scope.user.username,
      remember: $scope.remember
    };
    var params = {
    		 userName : $scope.user.username,
	         password : $scope.user.password,
	         vercode : $scope.user.code
    }
    var data = $.param(params)
    $http({  
    	url : '/login.do',
	    method: 'POST',
	    data : data,
	    headers: {'Content-Type': 'application/x-www-form-urlencoded','BsmAjaxHeader': true}
     }).success(function(obj){
   	  var data=obj.data;
      if (obj.success) {
      	envlist = data.envList;
      	auths = data.auths;
      	user = data.user;
        if ($scope.remember) {
          user.password = $scope.user.password;
        }
        data.data={
          "auths":auths,
        };
        var userData = user;
        $.extend(true,userData,user);
        localStorage.setItem('bycUserData', JSON.stringify(userData));
        localStorage.setItem('menuData', JSON.stringify(data.data.auths));
        //会话保存改用户可用环境信息
        sessionStorage.setItem('envlist', JSON.stringify(envlist));
        isAdmin = user.isAdmin;
        if(1 == isAdmin ){
        	//超级管理员默认环境信息为空
        	sessionStorage.setItem('currentEnvId',  null);
        }else if(0 == envlist.length){
        	//其他成员如果没有环境信息，不允许登录
        	$rootScope.pop('环境列空，请联系超级管理员','error');
        }else{
        	//会话保存当前默认环境信息
        	sessionStorage.setItem('currentEnvId',  envlist[0].envId);
        }
        $rootScope.userData = userData;
        $state.go('app.instrument.list');
        $rootScope.envlist=envlist;
        $rootScope.localEnv = envlist[0].envId;
      }else{
        $scope.pop(obj.message,'error');
        $scope.refreshCode();
      };
     }).error(function(data, status, headers, config) {
			$rootScope.isLoadingActive = false;
			switch (status){
				case 404:
					$rootScope.pop('请求未找到','error');
					break;
				case 500:
					$rootScope.pop('服务异常','error');
					break;
				case 401:
					$rootScope.pop('无权限','error');
					$state.go('access.login');
					break;
				default :
					$rootScope.pop('服务器异常','error');
			}
			var error = options.error;
			if (error && typeof error == 'function')
				error(data);
		});
    
/*    httpLoad.loadData({
      url : '/login.do',
      method : 'POST',
       noParam: true,
       data : {
         userName : $scope.user.username,
         password : $scope.user.password,
         vercode : $scope.user.code
       },
      success : function(obj) {
    	  var data=obj.data;
        if (obj.success) {
        	evlist = data.envList;
        	auths = data.auths;
          if ($scope.remember) {
            user.password = $scope.user.password;
          }
          
          data.data={
            "apiKey": "1214546782",
            "secKey": "1243538932",
            "auths":auths [
              {
                "name": "仪表盘",
                "actionUrl": "app.instrument.list",
                "icon": "fa fa-desktop icon-speedometer",
                "id": 1,
                "priority": 1
              },
              {
                "children": [
                  {
                    "name": "应用构建",
                    "actionUrl": "app.application.create",
                    "icon": null,
                    "id": 21,
                    "priority": 2,
                    "parentId": 2
                  },
                  {
                    "name": "应用部署",
                    "actionUrl": "app.application.develop",
                    "icon": null,
                    "id": 22,
                    "priority": 2,
                    "parentId": 2
                  }
                ],
                "name": "应用管理",
                "actionUrl": "app.application",
                "icon": "fa fa-desktop icon-cloud-download",
                "id": 2,
                "priority": 1
              },
              {
                "children": [
                  {
                    "name": "容器",
                    "actionUrl": "app.containerManger.container",
                    "icon": null,
                    "id": 31,
                    "priority": 2,
                    "parentId": 3
                  },
                  {
                    "name": "网络",
                    "actionUrl": "app.containerManger.net",
                    "icon": null,
                    "id": 32,
                    "priority": 2,
                    "parentId": 3
                  },
                  {
                    "name": "镜像",
                    "actionUrl": "app.containerManger.img",
                    "icon": null,
                    "id": 33,
                    "priority": 2,
                    "parentId": 3
                  },
                  {
                    "name": "硬盘卷",
                    "actionUrl": "app.containerManger.disk",
                    "icon": null,
                    "id": 34,
                    "priority": 2,
                    "parentId": 3
                  }
                ],
                "name": "容器管理",
                "actionUrl": "app.containerManger",
                "icon": "fa fa-desktop icon-home",
                "id": 3,
                "priority": 1
              },
              {
                "children": [
                  {
                    "name": "主机",
                    "actionUrl": "app.resources.host",
                    "icon": null,
                    "id": 41,
                    "priority": 2,
                    "parentId": 4
                  },
                  {
                    "name": "集群",
                    "actionUrl": "app.resources.swarm",
                    "icon": null,
                    "id": 42,
                    "priority": 2,
                    "parentId": 4
                  },
                  {
                    "name": "仓库",
                    "actionUrl": "app.resources.warehouse",
                    "icon": null,
                    "id": 43,
                    "priority": 2,
                    "parentId": 4
                  },
                  {
                    "name": "IP",
                    "actionUrl": "app.resources.ip",
                    "icon": null,
                    "id": 44,
                    "priority": 2,
                    "parentId": 4
                  }
                ],
                "name": "资源管理",
                "actionUrl": "app.resources",
                "icon": "fa fa-desktop icon-settings",
                "id": 4,
                "priority": 4
              },
              {
                "children": [
                  {
                    "name": "账户",
                    "actionUrl": "app.userCenter.account",
                    "icon": null,
                    "id": 51,
                    "priority": 2,
                    "parentId": 5
                  },
                  {
                    "name": "角色",
                    "actionUrl": "app.userCenter.role",
                    "icon": null,
                    "id": 52,
                    "priority": 2,
                    "parentId": 5
                  },
                  {
                    "name": "权限",
                    "actionUrl": "app.userCenter.permissions",
                    "icon": null,
                    "id": 53,
                    "priority": 2,
                    "parentId": 5
                  }
                ],
                "name": "用户中心",
                "actionUrl": "app.userCenter",
                "icon": "fa fa-desktop icon-users",
                "id": 5,
                "priority": 1
              }
            ],
            "roles": [
              {
                "gmtModify": "2016-09-08 11:34:20",
                "moreProp": null,
                "remark": "超级管理员",
                "gmtCreate": "2016-05-31 16:46:20",
                "ownerId": null,
                "menderId": 1,
                "isDeleted": false,
                "isLocked": false,
                "name": "sunqi",
                "tenantId": null,
                "createrId": 1,
                "id": 1,
                "status": null
              }
            ],
            "user": {
              "gmtModify": "2016-09-09 11:37:16",
              "mobile": "18330033981",
              "moreProp": null,
              "remark": "超级管理员",
              "sessionId": "D578CE33CE4C5D3ABED05672BE18CE93",
              "gmtCreate": "2016-03-23 17:48:53",
              "ownerId": null,
              "loginStatus": true,
              "menderId": 1,
              "password": "-2cc3fe75eb672eeb60a6a80aa0565466506d9d1e",
              "isDeleted": false,
              "tenantName": null,
              "phone": "010-1313131",
              "isTenant": false,
              "isLocked": false,
              "name": "sunqi",
              "tenantId": null,
              "company": null,
              "createrId": 1,
              "id": 1,
              "roleNames": null,
              "email": "admin@qq.com",
              "status": "NORMAL",
              "username": "admin"
            }
          };
          var userData = data.data.user;
          $.extend(true,userData,user);
          localStorage.setItem('bycUserData', JSON.stringify(userData));
          localStorage.setItem('menuData', JSON.stringify(data.data.auths));
          $rootScope.userData = userData;
          $state.go('app.instrument.list');
        }else{
          $scope.refreshCode();
        };
      }
    });*/
  };
  //刷新验证码
  $scope.refreshCode = function(){
    $scope.url = "/captcha?" + new Date().getTime();
    $scope.user.code = '';
  };
  $scope.refreshCode();
}]);
app.directive('ngAutoPos', ['httpLoad','$rootScope', function (httpLoad, $rootScope) {
  return {
    restrict: 'EA',
    scope:false,
    link: function (scope, element, attrs) {
      var setPos = function(){
        var top = ($(window).height() - 669)/2;
        $(element).css({"margin-top":top+"px"});
      };
      setPos();
      window.onresize = function(){
        setPos();
      }
    }
  }
}]);
